package com.oocl.scan.dao.impl;

import com.arangodb.ArangoCursor;
import com.arangodb.entity.BaseDocument;
import com.arangodb.util.MapBuilder;
import com.oocl.scan.dao.BasicDao;
import com.oocl.scan.dao.SoapConfigDao;
import com.oocl.scan.model.SoapConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SoapConfigDaoImpl extends BasicDao implements SoapConfigDao {

  private static final String SOAP_CONFIG = "soapConfig";

  @Override
  public void add(SoapConfig soapConfig) {
    SoapConfig soap = findByDtoAndURL(soapConfig.getDtoName(),soapConfig.getSoapConfigURL());
    if(soap == null){
      BaseDocument document = new BaseDocument();
      document.addAttribute("dtoName", soapConfig.getDtoName());
      document.addAttribute("soapConfigURL", soapConfig.getSoapConfigURL());
      document.addAttribute("resourceName", soapConfig.getResourceName());
      document.addAttribute("isOrigin", soapConfig.isOrigin());
      getDataBase().collection(SOAP_CONFIG).insertDocument(document);
    }else if( soap.getResourceName() == null){
      getDataBase().collection(SOAP_CONFIG).updateDocument(soap.get_key(),soapConfig);
    }
  }

  @Override
  public List<SoapConfig> getAll() {
    String query = "FOR t IN " + SOAP_CONFIG + " RETURN t";
    Map<String, Object> bindVars = new MapBuilder().get();
    ArangoCursor<SoapConfig> cursor = getDataBase().query(query, bindVars, null, SoapConfig.class);
    List<SoapConfig> soapConfigList = new ArrayList<>();
    while (cursor.hasNext()) {
      soapConfigList.add(cursor.next());
    }
    return soapConfigList;
  }

  @Override
  public List<SoapConfig> getAllOrigin() {
    List<SoapConfig> soapConfigList = new ArrayList<>();
    for (SoapConfig soapConfig : getAll()){
      if(soapConfig.isOrigin()){
        soapConfigList.add(soapConfig);
      }
    }
    return soapConfigList;
  }

  @Override
  public List<SoapConfig> getByDtoName(String dtoName) {
    String query = "FOR t IN " + SOAP_CONFIG + " FILTER t.dtoName == @dtoName RETURN t";
    Map<String, Object> bindVars = new MapBuilder().put("dtoName", dtoName).get();
    ArangoCursor<SoapConfig> cursor = getDataBase().query(query, bindVars, null, SoapConfig.class);
    List<SoapConfig> soapConfigList = new ArrayList<>();
    while (cursor.hasNext()) {
      soapConfigList.add(cursor.next());
    }
    return soapConfigList;
  }

  @Override
  public SoapConfig getOriginByDtoName(String dtoName) {
    String query = "FOR t IN " + SOAP_CONFIG + " FILTER t.dtoName like @dtoName FILTER t.isOrigin == @isOrigin RETURN t";
    Map<String, Object> bindVars = new MapBuilder().put("dtoName", "%"+dtoName).put("isOrigin",true).get();
    ArangoCursor<SoapConfig> cursor = getDataBase().query(query, bindVars, null, SoapConfig.class);
    SoapConfig soapConfig = null;
    while (cursor.hasNext()) {
      soapConfig = cursor.next();
      break;
    }
    return soapConfig;
  }

  @Override
  public void delete(String key) {
    getDataBase().collection(SOAP_CONFIG).deleteDocument(key);
  }

  private SoapConfig findByDtoAndURL(String dtoName,String url) {
    String query = "FOR t IN " + SOAP_CONFIG + " FILTER t.dtoName == @dtoName FILTER t.soapConfigURL == @soapConfigURL RETURN t";
    Map<String, Object> bindVars = new MapBuilder().put("dtoName", dtoName).put("soapConfigURL",url).get();
    ArangoCursor<SoapConfig> cursor = getDataBase().query(query, bindVars, null, SoapConfig.class);
    SoapConfig soapConfig = null;
    while (cursor.hasNext()) {
      soapConfig = cursor.next();
      break;
    }
    return soapConfig;
  }

}
